package day14;

//1.判断是否有包子,决定当前线程是否执行
// 2.如果没有包子,就进入等待状态,如果有包子,就消费包子
// 3.消费包子后,更新桌子上包子状态,唤醒生产者生产包子
public class Foodie extends Thread {
    private Desk desk;
    Foodie(Desk desk){
        this.desk=desk;
    }
    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                if (desk.getCount() == 0) {
                    break;

                } else {
                    if (desk.isFlag()) {
                        System.out.println("消费者正在吃包子");
                        desk.setFlag(false);
                        desk.getLock().notifyAll();
                        desk.setCount(desk.getCount()-1);
                    } else {
                        try {
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }

        }
    }
}
